package com.chefu.carowner.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chefu.carowner.mapper.CarUserMapper;
import com.chefu.carowner.pojo.CarUser;
import com.chefu.carowner.service.CarUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chefu.common.constants.CfConstants;
import com.chefu.common.constants.MQConstants;
import com.chefu.common.exception.CfException;
import com.chefu.common.exception.ExceptionEnum;
import com.chefu.common.pojo.PageResult;
import com.chefu.common.utils.auth.UserHolder;
import com.chefu.common.utils.auth.UserInfo;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author goodgoodstudy
 * @since 2021-03-30
 */
@Service
public class CarUserServiceImpl extends ServiceImpl<CarUserMapper, CarUser> implements CarUserService {

    @Autowired
    AmqpTemplate amqpTemplate;
    @Autowired
    StringRedisTemplate redisTemplate;
    @Autowired
    PasswordEncoder passwordEncoder;

    /**
     * 发送手机验证码
     */
    @Override
    public void sendVerifyCode(String phone) {
        String code = RandomStringUtils.randomNumeric(6);

        //保存到缓存并设置失效时间(5分钟)
        redisTemplate.opsForValue().set(CfConstants.REDIS_KEY_PRE + phone, code, 5, TimeUnit.MINUTES);

        //发送信息给mq
        Map<String, String> smsMap = new HashMap<>();
        smsMap.put("phone", phone);
        smsMap.put("code", code);
        amqpTemplate.convertAndSend(MQConstants.Exchange.SMS_EXCHANGE_NAME, MQConstants.RoutingKey.VERIFY_CODE_KEY, smsMap);
    }

    /**
     * 修改车主信息
     */
    @Override
    public void updateCarOwner(CarUser carUser) {
        UserInfo user = UserHolder.getUser();
        Long id = user.getId();
        carUser.setCuId(id);
        carUser.setPassword(null);

        updateById(carUser);
    }

    /**
     * 修改密码
     */
    @Override
    public void modifyPwd(String oldPwd, String newPwd) {
        UserInfo user = UserHolder.getUser();
        CarUser carUser = getOne(new QueryWrapper<CarUser>().eq("cu_id", user.getId()).select("password"));
        if(!passwordEncoder.matches(oldPwd, carUser.getPassword())){
            throw new CfException(ExceptionEnum.FAIL_OLD_PASSWORD);
        }
        carUser.setPassword(passwordEncoder.encode(newPwd));
        updateById(carUser);
    }

    /**
     * 查询车主列表
     */
    @Override
    public PageResult<CarUser> findUserByPage(Long pageNo, Long pageSize, String name, String phone, Integer status) {

        Page<CarUser> userPage = new Page<>(pageNo, pageSize);

        QueryWrapper<CarUser> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(name)){
            queryWrapper.like("real_name", name);
        }
        if (StringUtils.isNotEmpty(phone)){
            queryWrapper.likeRight("phone", phone);
        }
        if (null != status){
            queryWrapper.eq("status", status);
        }
        page(userPage, queryWrapper);

        return new PageResult<>(userPage.getTotal(), userPage.getPages(), userPage.getRecords());
    }


}
